/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.gzchengguo.cn

 */
package link.chengguo.orangemall.modules.activity.rest;

import cn.hutool.core.util.ObjectUtil;
import link.chengguo.orangemall.logging.aop.log.Log;
import link.chengguo.orangemall.modules.activity.domain.OrangeStoreCombination;
import link.chengguo.orangemall.modules.activity.service.OrangeStoreCombinationService;
import link.chengguo.orangemall.modules.activity.service.dto.OrangeStoreCombinationQueryCriteria;
import link.chengguo.orangemall.modules.aop.ForbidSubmit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author chengguo
* @date 2019-11-18
*/
@Api(tags = "商城:拼团管理")
@RestController
@RequestMapping("api")
public class StoreCombinationController {

    private final OrangeStoreCombinationService orangeStoreCombinationService;

    public StoreCombinationController(OrangeStoreCombinationService orangeStoreCombinationService) {
        this.orangeStoreCombinationService = orangeStoreCombinationService;
    }

    @Log("查询拼团")
    @ApiOperation(value = "查询拼团")
    @GetMapping(value = "/orangeStoreCombination")
    @PreAuthorize("hasAnyRole('admin','ORANGESTORECOMBINATION_ALL','ORANGESTORECOMBINATION_SELECT')")
    public ResponseEntity getOrangeStoreCombinations(OrangeStoreCombinationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(orangeStoreCombinationService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @Log("修改拼团")
    @ApiOperation(value = "新增/修改拼团")
    @PutMapping(value = "/orangeStoreCombination")
    @PreAuthorize("hasAnyRole('admin','ORANGESTORECOMBINATION_ALL','ORANGESTORECOMBINATION_EDIT')")
    public ResponseEntity update(@Validated @RequestBody OrangeStoreCombination resources){
        if(ObjectUtil.isNull(resources.getId())){
            return new ResponseEntity<>(orangeStoreCombinationService.save(resources),HttpStatus.CREATED);
        }else{
            orangeStoreCombinationService.saveOrUpdate(resources);
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }

    }

    @ForbidSubmit
    @ApiOperation(value = "开启关闭")
    @PostMapping(value = "/orangeStoreCombination/onsale/{id}")
    public ResponseEntity onSale(@PathVariable Long id,@RequestBody String jsonStr){
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        Integer status = jsonObject.getInteger("status");
        orangeStoreCombinationService.onSale(id,status);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("删除拼团")
    @ApiOperation(value = "删除拼团")
    @DeleteMapping(value = "/orangeStoreCombination/{id}")
    @PreAuthorize("hasAnyRole('admin','ORANGESTORECOMBINATION_ALL','ORANGESTORECOMBINATION_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        orangeStoreCombinationService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
